package com.jqk.cloudzuul.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author:JiangQiKun
 * @Date:2022/03/13 02:28 PM
 * Description:
 * com.jqk.cloudzuul.filterIPFilter
 *
 * ip限制，ip调用次数限制，设备号过滤
 */
public class IPFilter extends ZuulFilter {
    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        RequestContext context = RequestContext.getCurrentContext();
        HttpServletRequest request = context.getRequest();
        // 1:获取ip 地址

        // 2:访问 redis ,拿到访问次数，做对比

        // 3：如果达到了访问次数，设置不向后面的过滤器转发，返回 null;否则继续执行
        // ps：后面的过滤器可以根据 RequestContext.getCurrentContext().sendZuulResponse();
        // 不向后面的其他过滤器转发，但是还是会把所有的 pre 过滤器执行一遍
        context.setSendZuulResponse(false);

        return null;
    }
}
